﻿var ns, toast, actionsheet, WXPhotoPicker;
var app = new Vue({
    el: '#app',
    data: {
        ID:0,
        form: {
            ID: 0,
            PointID: '',//---配送点id
            PointName:'',//配送点名称
            ServiceContent: '',//备注信息
        },
        outTypemsg:[],
        IsRoomOpen:false,
        imglist: [],
        maxuploadcount: CONFIG.maxuploadcount,
    },
    methods: {
        //获取配送点列表接口
        getOutType:function(){
            var that = this;
            ns.post({
                action: 'getorderpointlist',
                ID:that.ID,
            }, function(succeed, data, err) {
                // console.log(JSON.stringify(data));
                // console.log(JSON.stringify(err));
                if (succeed) {
                    if(data.status){
                        that.outTypemsg=data.data.PointList; 
                        if (that.outTypemsg.length > 0) {
                            that.doSelectRoom(that.outTypemsg[0]);
                        }  
                    }
                    else{
                        api.toast({
                            msg: data.Msg,
                            duration: 2000,
                            location: 'bottom'
                        });

                         //刷新并关闭
                         setTimeout(() => {
                            api.sendEvent({
                                name: 'relod_orderlist',
                            });
                            api.closeWin();
                        }, 1000);
                        return;
                    }             
                }
            });
        },
        do_save: function () {
            var that = this;
            if (!that.form.PointName) {
                api.toast({
                    msg: '请选择配送点',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }   
          
            var filelist = [];
            for (var i = 0; i < that.imglist.length; i++) {
                filelist.push(that.imglist[i].url);
            }

            if (filelist.length<=0) {
                api.toast({
                    msg: '请上传至少一张图片',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }   

            var options = {};
            options.action = "acceptorderpoint";
            options.ID = that.ID;
            options.PointID = that.form.PointID;
            options.PointName = that.form.PointName;
            options.PlantFormReceiveRemark = that.form.ServiceContent;

            // console.log(JSON.stringify(options));
            toast.loading({
                title: "提交中",
                duration: 2000
            }, function (ret) { });
            ns.post(options, function (succeed, data, err) {
                // console.log(JSON.stringify(err));
                // console.log(JSON.stringify(data));
                toast.hide();
                if (succeed) {  
                    if(data.status){
                        api.toast({
                            msg: '操作成功',
                            duration: 2000,
                            location: 'bottom'
                        });
                    }   
                    else{
                        api.toast({
                            msg: data.Msg,
                            duration: 2000,
                            location: 'bottom'
                        });   
                        return;
                    }  

                    //刷新并关闭
                    setTimeout(() => {
                        api.sendEvent({
                            name: 'relod_orderlist',
                        });
                        api.closeWin();
                    }, 1000);
                    return;

                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                files: {
                    file: filelist
                }
            });
        },
        do_choose_type: function () {
            var that = this;
            that.IsRoomOpen=true;
        },
        doCloseRoom:function(){
            var that=this;
            that.IsRoomOpen=false;
        },
        choose_picture: function () {
            var that = this;
            ns.confirmPer('camera', function () {
                that.choose_picture_start();
            })
        },
        choose_picture_start:function(){
            var that = this;
            if (that.imglist.length >= that.maxuploadcount) {
                api.toast({
                    msg: '最多支持' + that.maxuploadcount + '张图片',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            api.actionSheet({
                cancelTitle: '取消',
                buttons: ['拍照', '从相册选择']
            }, function(ret, err) {
                //拍照
                if(ret.buttonIndex==1){ 
                    that.upload_choose_picture();
                    return;
                }
                //从相册选择
                if(ret.buttonIndex==2){
                    that.choose_picture_album();
                    return;
                } 
                //取消 
                else{
                    return false;  
                }  
            });
        },
        upload_choose_picture:function(){
            var that = this;
            api.getPicture({
                sourceType: 'camera',
                encodingType: 'jpg',
                destinationType: 'url',
                allowEdit: false,
                saveToPhotoAlbum: true,
            }, function(ret, err) {
                if (ret&&ret.data) {
                    that.imglist.push({
                        ID: 0,
                        url: ret.data,
                        thumbPath: ret.data,
                        isupload: true
                    });
                } 
            });
        },
        choose_picture_album:function(){
            var that = this;
            WXPhotoPicker.open({
                max: that.maxuploadcount - that.imglist.length,
                type:'image',
            }, function(ret) {
                if (ret) {
                    if (ret.list.length > 0) {
                        ret.list.forEach((item) => {
                            that.imglist.push({
                                ID: 0,
                                url: item.path,
                                thumbPath: item.path,
                                isupload: true
                            });
                        });
                    }
                }
            });
        },
        remove_img: function (index,item) {
            var that = this;
            that.imglist.splice(index, 1);
        },
        do_viewimages: function (activeIndex) {
            var that = this;
            var imageUrls = [];
            for (var i = 0; i < that.imglist.length; i++) {
                imageUrls.push(that.imglist[i].url);
            }
            var imageBrowser = api.require('imageBrowser');
            imageBrowser.openImages({
                showList: false,
                activeIndex: activeIndex,
                imageUrls: imageUrls
            });
        },
        doSelectRoom:function(item){
            var that=this;
            that.IsRoomOpen=false;
            that.form.PointID=item.ID;
            that.form.PointName=item.SendPoint;
        },
    }
});

apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    toast = new auiToast();
    actionsheet = new auiActionsheet();
    WXPhotoPicker = api.require('WXPhotoPicker');
    app.ID = api.pageParam.id;
  
    app.getOutType();
  
};
